import 'package:shared_preferences/shared_preferences.dart';
import '../../core/api_response.dart';
import '../../providers/mood_provider.dart';
import '../../models/love_quote.dart';
import '../mood_repository.dart';
import '../../services/mock_api_service.dart';

class MockMoodRepository implements MoodRepository {
  @override
  Future<ApiResponse<MoodType?>> getMyMood() async {
    return MockApiService.simulateApiCall<MoodType?>(() async {
      final prefs = await SharedPreferences.getInstance();
      final moodIndex = prefs.getInt('my_mood');
      
      if (moodIndex != null && moodIndex < MoodType.values.length) {
        return MoodType.values[moodIndex];
      }
      
      return null;
    });
  }
  
  @override
  Future<ApiResponse<MoodType?>> getPartnerMood() async {
    return MockApiService.simulateApiCall<MoodType?>(() async {
      final prefs = await SharedPreferences.getInstance();
      final moodIndex = prefs.getInt('partner_mood');
      
      if (moodIndex != null && moodIndex < MoodType.values.length) {
        return MoodType.values[moodIndex];
      }
      
      return null;
    });
  }
  
  @override
  Future<ApiResponse<DateTime>> getLastMoodUpdate() async {
    return MockApiService.simulateApiCall<DateTime>(() async {
      final prefs = await SharedPreferences.getInstance();
      final lastUpdateString = prefs.getString('last_mood_update');
      
      if (lastUpdateString != null) {
        return DateTime.parse(lastUpdateString);
      }
      
      return DateTime.now();
    });
  }
  
  @override
  Future<ApiResponse<void>> setMyMood(MoodType mood) async {
    return MockApiService.simulateVoidApiCall(() async {
      final prefs = await SharedPreferences.getInstance();
      final now = DateTime.now();
      
      await prefs.setInt('my_mood', mood.index);
      await prefs.setString('last_mood_update', now.toIso8601String());
    });
  }
  
  @override
  Future<ApiResponse<void>> setPartnerMood(MoodType mood) async {
    return MockApiService.simulateVoidApiCall(() async {
      final prefs = await SharedPreferences.getInstance();
      final now = DateTime.now();
      
      await prefs.setInt('partner_mood', mood.index);
      await prefs.setString('last_mood_update', now.toIso8601String());
    });
  }
  
  @override
  Future<ApiResponse<LoveQuote>> getDailyLoveQuote() async {
    return MockApiService.simulateApiCall<LoveQuote>(() async {
      final quotes = [
        '你是我心中最亮的星星 ⭐',
        '和你在一起的每一天都是最好的一天 💕',
        '你的笑容是我最喜欢的风景 😊',
        '爱你不是三分钟热度，而是深思熟虑后的决定 💖',
        '你是我的小确幸，也是我的大梦想 🌟',
        '想和你一起慢慢变老 👫',
        '你是我的今天，也是我所有的明天 🌅',
        '遇见你是我最美丽的意外 🎁',
      ];
      
      final today = DateTime.now();
      final index = (today.year + today.month + today.day) % quotes.length;
      final selectedQuote = quotes[index];
      
      return LoveQuote(
        id: index + 1,
        content: selectedQuote,
        category: 'daily',
        language: 'zh_CN',
        moodTags: '爱情,甜蜜',
        usageCount: 1,
        isActive: 1,
        createdBy: 'system',
        createdAt: DateTime.now().millisecondsSinceEpoch,
      );
    });
  }
}